#include "myhead.h"

//播放
int Music_Play(pD_music music_head)
{
    char buf[1024];
    musiclist = 1;
    pD_music p = music_head->next;

    //开始播放
    if (mplayer == 0)
    {
        // 遍历到指定结点
        for (int i = 0; i < video_num-1; i++)
        {
            p = p->next;
        }        
        //开始播放后显示播放图标
        ShowJpg(map, "pic/music/Play.jpg", 370, 420, 1, 1);
        memset(buf, 0, sizeof(buf));
        sprintf(buf, "mplayer -quiet -slave -loop 0 -input file=/tmp/fifo -geometry 80:20 -zoom -x 570 -y 380 -ss 0 %s &",p->name);
        //保存当前视频的编号
        video_num = p->num;
        fp_mplayer = popen(buf, "r");
        if (fp_mplayer == NULL)
        {
            perror("popen fail");
            return -1;
        }

        //播放列表
        Show_List(&musiclist , music_head);

        mplayer = 1;
    }
    else if (mplayer == 1 && mplayer_stop == 0)//暂停
    {
        //暂停图标
        ShowJpg(map , "pic/music/Time_out.jpg" , 370 , 420 , 1 , 1);

        pthread_mutex_lock(&mutex);//上锁
        mplayer_stop = 1;
        Send_Command("pause\n");
        pthread_mutex_unlock(&mutex);//解锁
		printf("mplayer_stop:%d\n",mplayer_stop);
    }
    else if (mplayer == 1 && mplayer_stop == 1)//继续
    {
        ShowJpg(map,"pic/music/Play.jpg",370,420,1,1);
        Send_Command("pause\n");
        mplayer_stop = 0;
    }

    return 0;
}


void Show_List(int *lst , pD_music music_head)
{
    int i = 0;                     // 循环计数器i
    int y = 0;                     // 垂直位置计数器y
    int count = 9;                 // 内循环计数器count
    char buf[10] = {0};            // 字符串缓冲区buf，用于存储转换后的数字字符串
    pD_music p = music_head->next; // 指向音乐链表头节点的下一个节点

    // 根据传入的lst参数确定遍历的起始位置，将p指针定位到对应节点
    for(i = *lst; i > 1; i--)
    {
        while(count--) // 内循环遍历count个节点
        {
            p = p->next;
        }
    }

    // 显示列表背景图片
    ShowJpg(map,"pic/music/list_ground.jpg",650,50,1,1);

    // 将lst值转换为字符串，并显示在界面上
    sprintf(buf,"%d",*lst);
    ShowJpg(map,"pic/music/list_num.jpg",705,360,1,1);
    Display_characterX(710,375,buf,0x00000000,2);

    // 遍历播放列表并显示在界面上
    for(y = 60; y <= 300; y = y + 30)
    {
        Display_characterX(650, y, p->name, 0x00ffffff, 1); // 显示音乐名称
        p = p->next; // 指向下一个节点
        if(p == music_head) // 如果p指向头节点，表示遍历结束
        {
            break;
        }
    }
}

//上一首
void Music_prev(pD_music music_head)
{
    pD_music p = music_head;
    char buf[1024];

    // 遍历到指定结点
    for (int i = 0; i < video_num; i++)
    {
        p = p->next;
    }
    p = p->prev;
    if(p == music_head)
    {
        p = p->prev;
    }
    ShowJpg(map, "pic/music/Play.jpg", 370, 420, 1, 1);

    pthread_mutex_lock(&mutex); // 上锁

    Send_Command("q\n");
    music_next = 1;

    // 播放当前链表结点的视频
    memset(buf, 0, sizeof(buf));
    sprintf(buf, "mplayer -quiet -slave -loop 0 -input file=/tmp/fifo -geometry 80:20 -zoom -x 570 -y 380 -idx  %s", p->name);
    video_num = p->num;
	
    fp_mplayer = popen(buf, "r");
	
    pthread_mutex_unlock(&mutex); // 解锁
}

//下一首
void Music_Next(pD_music music_head)
{
    pD_music p = music_head;
    char buf[1024];

    // 遍历到指定结点
    for (int i = 0; i < video_num; i++)
    {
        p = p->next;
    }
    p = p->next;
    if(p == music_head)
    {
        p = p->next;
    }
    ShowJpg(map, "pic/music/Play.jpg", 370, 420, 1, 1);

    pthread_mutex_lock(&mutex); // 上锁
	
    Send_Command("q\n");
    music_next = 1;

    // 播放当前链表结点的视频
    memset(buf, 0, sizeof(buf));
    sprintf(buf, "mplayer -quiet -slave -loop 0 -input file=/tmp/fifo -geometry 80:20 -zoom -x 570 -y 380 -idx  %s", p->name);
    video_num = p->num;
	
    fp_mplayer = popen(buf, "r");

    pthread_mutex_unlock(&mutex); // 解锁


    return;
}

//快进  后退
void Music_Seek(int mv_val)
{
    char buf[50] = {0};
    
    sprintf(buf,"seek %d\n",mv_val);

    pthread_mutex_lock(&mutex);//上锁
    Send_Command(buf);
    if(mv_val < 0)
    {
        music_move = 1;
    }
    pthread_mutex_unlock(&mutex);//解锁

    if(mplayer_stop == 1)
    {
        Send_Command("pause\n");
    }

    return;
}

//上一页
int Prev_List(int *musiclist , pD_music music_head)
{
    printf("musiclist:%d",*musiclist);
    if((*musiclist) == 1)
    {
        return 0;
    }
    else
    {
        (*musiclist)--;
    }
    
    Show_List(musiclist , music_head);

    return 0;
}

//下一页
int Next_List(int *musiclist , pD_music music_head)
{
    printf("musiclist:%d",*musiclist);
    int video_list = 0;

    if((count%9)>0)
    {
        video_list = (count/9)+1;
    }
    else
    {
        video_list = (count/9);
    }

    if((*musiclist) < video_list)
    {
        (*musiclist)++;
    }
    else
    {
        return 0;
    }

    Show_List(musiclist , music_head);

    return 0;
}

//倍速
void Music_Speed(pD_music music_head)
{
    int x = 0;
    int y = 0;
    int video_lst = 0;
    musiclist = 1;

    while(1)
    {
        ShowJpg(map, "pic/music/beisu_ground.jpg", 0, 220, 1, 1);
        Display_characterX(0,230,"0.5X",0x00ffffff,2);
        Display_characterX(0,280,"1.0X",0x00ffffff,2);
        Display_characterX(0,330,"2.0X",0x00ffffff,2);

        Get_side();
        if(x_s>=0 && x_e<80 && y_s>=230 && y_e<280)
        {
            printf("0.5\n");
            pthread_mutex_lock(&mutex);//上锁
            Send_Command("speed_set 0.5\n");
            pthread_mutex_unlock(&mutex);//解锁
        }
        else if (x_s>=0 && x_e<80 && y_s>=280 && y_e<330)
        {
            printf("1\n");
            pthread_mutex_lock(&mutex);//上锁
            Send_Command("speed_set 1\n");
            pthread_mutex_unlock(&mutex);//解锁
        }
        else if(x_s>=0 && x_e<80 && y_s>=330 && y_e<380)
        {
            printf("2\n");
            pthread_mutex_lock(&mutex);//上锁
            Send_Command("speed_set 2\n");
            pthread_mutex_unlock(&mutex);//解锁
        }

        if(mplayer_stop == 1)
        {
            Send_Command("pause\n");
        }
        
        ShowJpg(map, "pic/music/beisu_ground.jpg", 0, 220, 1, 1);

        //播放列表
        Show_List(&musiclist , music_head);

        break; 
    }
}

//静音或者开声音
void Mute_Volume()
{
    if(mplayer_mute == 0)
    {
        pthread_mutex_lock(&mutex);//上锁
        Send_Command("mute 1\n");
        mplayer_mute = 1;
        pthread_mutex_unlock(&mutex);//解锁
        
        ShowJpg(map,"pic/music/mute.jpg",0,420,1,1);
    }
    else
    {
        pthread_mutex_lock(&mutex);//上锁
        Send_Command("mute 0\n");
        if(mplayer_stop == 1)
        {
            Send_Command("pause\n");
        }
        mplayer_mute = 0;
        pthread_mutex_unlock(&mutex);//解锁
        
        ShowJpg(map,"pic/music/volume.jpg",0,420,1,1);
    }
}

//暂停
int VStatus()
{
    //暂停图标
    ShowJpg(map , "pic/music/Time_out.jpg" , 370 , 420 , 1 , 1);

    pthread_mutex_lock(&mutex);//上锁
    mplayer_stop = 1;
    Send_Command("pause\n");
    pthread_mutex_unlock(&mutex);//解锁
}

//调节亮度
int VBright(int brightness)
{
    // sendCmd(fd_fifo, "brightness +1\n");
    if(brightness>10)  brightness=10;
    if(brightness<-10) brightness=-10;
    char str[256];
    if(brightness>0) sprintf(str,"brightness +%d\n",brightness);
    // else sprintf(str,"brightness -%d\n",brightness);
    sprintf(str,"brightness %d\n",brightness);
    printf("%s\n",str);
    Send_Command(str);

    return 0;
}


//寻找mv播放
int Mv_Play(pD_music music_head , pD_music video_head)
{
    pD_music p = music_head->next;
    pD_music pp = video_head->next;
    char buf[1024];
    // 遍历到指定结点
    for (int i = 0; i < video_num-1; i++)
    {
        p = p->next;
    }

    //这时已经遍历到了当前播放的mp3的结点
    while (pp != NULL)
    {
        // 取得p->name和pp->name的扩展名之前的部分
        char *dot = strrchr(p->name, '.');
        char *dot2 = strrchr(pp->name, '.');
        if (dot != NULL && dot2 != NULL)
        {
            int len = dot - p->name;
            char name[len + 1];
            char name2[len + 1];
            strncpy(name, p->name, len);
            strncpy(name2, pp->name, len);
            name[len] = '\0';
            name2[len] = '\0';

            // 将两个文件名都向后移动4个字符
            char *name_shifted = name + 6;
            char *name2_shifted = name2 + 6;
            printf("name:%s name2:%s\n",name_shifted,name2_shifted);
            ShowJpg(map,"pic/music/mv_ground.jpg",650,420,1,1);

            // 比较nameWithoutExtension和pp->name的扩展名之前的部分
            if (strcmp(name_shifted, name2_shifted) == 0)
            {
                ShowJpg(map,"pic/music/mv_success.jpg",650,420,1,1);
                printf("找到了\n");
                //播放mv
                pthread_mutex_lock(&mutex); // 上锁

                Send_Command("q\n");
                music_next = 1;
                //播放当前链表结点的视频
                memset(buf, 0, sizeof(buf));
                sprintf(buf, "mplayer -quiet -slave -loop 0 -input file=/tmp/fifo -geometry 80:20 -zoom -x 570 -y 380 -idx  %s", pp->name);
                video_num = p->num;
                fp_mplayer = popen(buf, "r");

                pthread_mutex_unlock(&mutex); // 解锁
                // 退出循环
                break;
            }
        }

        pp = pp->next;

        if(pp->next == video_head)
        {
            printf("没找到\n");
            ShowJpg(map,"pic/music/mv_error.jpg",650,420,1,1);
            break;
        }
    }
    
    return 0;
}